

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>scitools.FloatComparison &mdash; SciTools 0.8.3 documentation</title>
    
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '0.8.3',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="top" title="SciTools 0.8.3 documentation" href="index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="np-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="index.html">SciTools 0.8.3 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-scitools.FloatComparison">
<span id="scitools-floatcomparison"></span><h1><a class="reference internal" href="#module-scitools.FloatComparison" title="scitools.FloatComparison"><tt class="xref py py-mod docutils literal"><span class="pre">scitools.FloatComparison</span></tt></a><a class="headerlink" href="#module-scitools.FloatComparison" title="Permalink to this headline">¶</a></h1>
<dl class="class">
<dt id="scitools.FloatComparison.FloatComparison">
<em class="property">class </em><tt class="descclassname">scitools.FloatComparison.</tt><tt class="descname">FloatComparison</tt><big>(</big><em>operation='=='</em>, <em>rtol=1e-14</em>, <em>atol=1e-14</em><big>)</big><a class="reference internal" href="_modules/scitools/FloatComparison.html#FloatComparison"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#scitools.FloatComparison.FloatComparison" title="Permalink to this definition">¶</a></dt>
<dd><p>Class FloatComparison is used to test
a == b, a &lt; b, a &lt;= b, a &gt; b, a &gt;= b and a != b
when a and b are floating-point numbers, complex numbers,
or NumPy arrays.</p>
<p>Because of possible round-off errors in the numbers, the tests are
performed approximately with a prescribed tolerance.</p>
<p>For example, a==b is true if abs(a-b) &lt; atol + rtol*abs(b).
The atol parameter comes into play when <a href="#id1"><span class="problematic" id="id2">|a|</span></a> and <a href="#id3"><span class="problematic" id="id4">|b|</span></a> are large.
(It would be mathematically more appealing to have
rtol*max(abs(a), abs(b)), but float_eq is used with a is
close to b so the max function is not necessary.)</p>
<p>If the desired test is <a href="#id5"><span class="problematic" id="id6">|a-b|</span></a> &lt; eps, set atol=eps and rtol=0.
If a relative test is wanted, <a href="#id7"><span class="problematic" id="id8">|(a-b)/b|</span></a> &lt; eps, set atol=0
and rtol=eps.</p>
<p>The test a &lt; b is performed as a &lt; b + atol (a can be
larger than b, but not more than atol).
A corresponding relative test reads a/abs(b) &lt; 1 + rtol.
These are combined into a common test a &lt; b + atol + rtol*abs(b).
Similarly, a &gt; b if a &gt; b - atol (i.e., a can be less than b,
but not less than b-atol). The relative test is then
a/abs(b) &gt; 1 - rtol. These are combined to
a &gt; b - a tol - rtol*abs(b). The &gt;= and &lt;= operators
are the same as &gt; and &lt; when tolerances are used.</p>
<p>Class FloatComparison can be used directly, or the convenience
names float_eq, float_ne, float_lt, float_le, float_gt and float_ge
for the various operators can be used instead. For example,
float_eq is a FloatComparison object for the equality operator.</p>
<p>Here is an interactive example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">FloatComparison</span> <span class="kn">import</span> <span class="n">FloatComparison</span><span class="p">,</span> <span class="n">float_eq</span><span class="p">,</span>              <span class="n">float_ne</span><span class="p">,</span> <span class="n">float_lt</span><span class="p">,</span> <span class="n">float_le</span><span class="p">,</span> <span class="n">float_gt</span><span class="p">,</span> <span class="n">float_ge</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">float_eq</span><span class="o">.</span><span class="n">get_absolute_tolerance</span><span class="p">()</span>   <span class="c"># default</span>
</pre></div>
</div>
<p>1e-14
&gt;&gt;&gt; float_eq.get_relative_tolerance()   # default
1e-14
&gt;&gt;&gt; float_eq.set_absolute_tolerance(1E-2)
&gt;&gt;&gt; float_eq.set_relative_tolerance(1E-2)
&gt;&gt;&gt; print float_eq
a == b, computed as abs(a-b) &lt; 0.01 + 0.01*abs(b)
&gt;&gt;&gt; 
&gt;&gt;&gt; float_eq(2.1, 2.100001)
True
&gt;&gt;&gt; # tolerances can be given as part of the test:
&gt;&gt;&gt; float_ne(2.1, 2.100001, atol=1E-14, rtol=1E-14)
True</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">float_gt</span><span class="p">(</span><span class="mf">2.0999999</span><span class="p">,</span> <span class="mf">2.1000001</span><span class="p">)</span>      <span class="c"># not greater with strict tol</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">float_gt</span>
<span class="go">a &gt; b, computed as a &gt; b - 1e-14 - 1e-14*abs(b)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">float_gt</span><span class="o">.</span><span class="n">set_absolute_tolerance</span><span class="p">(</span><span class="mf">1E-4</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">float_gt</span>
<span class="go">a &gt; b, computed as a &gt; b - 0.0001 - 1e-14*abs(b)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">float_gt</span><span class="p">(</span><span class="mf">2.0999999</span><span class="p">,</span> <span class="mf">2.1000001</span><span class="p">)</span>      <span class="c"># greater with less strict tol</span>
<span class="go">True</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">2.1</span><span class="p">,</span>      <span class="mf">2.1000001</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">2.100001</span><span class="p">,</span> <span class="mf">2.0999999</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">float_eq</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">float_lt</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>  <span class="c"># not less with strict tol</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">float_lt</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">atol</span><span class="o">=</span><span class="mf">1E-2</span><span class="p">,</span> <span class="n">rtol</span><span class="o">=</span><span class="mf">1E-2</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c"># use class FloatComparison directly:</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span> <span class="o">=</span> <span class="n">FloatComparison</span><span class="p">(</span><span class="s">&#39;==&#39;</span><span class="p">,</span> <span class="n">atol</span><span class="o">=</span><span class="mf">1E-3</span><span class="p">,</span> <span class="n">rtol</span><span class="o">=</span><span class="mf">1E-3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="p">(</span><span class="mf">2.1</span><span class="p">,</span> <span class="mf">2.100001</span><span class="p">)</span>     <span class="c"># __call__ directs to compare.eq</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="o">.</span><span class="n">gt</span><span class="p">(</span><span class="mf">2.1</span><span class="p">,</span> <span class="mf">2.100001</span><span class="p">)</span>  <span class="c"># same tolerance</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">compare</span><span class="o">.</span><span class="n">ge</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
<p>The __call__ method calls eq, ne, lt, le, gt, or ge, depending on
the first argument to the constructor.</p>
<p class="rubric">Methods</p>
<table border="1" class="longtable docutils">
<colgroup>
<col width="10%" />
<col width="90%" />
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td><tt class="xref py py-obj docutils literal"><span class="pre">__call__</span></tt>(a,&nbsp;b[,&nbsp;rtol,&nbsp;atol])</td>
<td>Compares a with b: a == b, a!= b, a &lt; b, etc., depending</td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="#scitools.FloatComparison.FloatComparison.eq" title="scitools.FloatComparison.FloatComparison.eq"><tt class="xref py py-obj docutils literal"><span class="pre">eq</span></tt></a>(a,&nbsp;b[,&nbsp;rtol,&nbsp;atol])</td>
<td>Tests a == b with tolerance.</td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="#scitools.FloatComparison.FloatComparison.ge" title="scitools.FloatComparison.FloatComparison.ge"><tt class="xref py py-obj docutils literal"><span class="pre">ge</span></tt></a>(a,&nbsp;b[,&nbsp;rtol,&nbsp;atol])</td>
<td>Tests a &gt;= b with tolerance.</td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="#scitools.FloatComparison.FloatComparison.get_absolute_tolerance" title="scitools.FloatComparison.FloatComparison.get_absolute_tolerance"><tt class="xref py py-obj docutils literal"><span class="pre">get_absolute_tolerance</span></tt></a>()</td>
<td></td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="#scitools.FloatComparison.FloatComparison.get_relative_tolerance" title="scitools.FloatComparison.FloatComparison.get_relative_tolerance"><tt class="xref py py-obj docutils literal"><span class="pre">get_relative_tolerance</span></tt></a>()</td>
<td></td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="#scitools.FloatComparison.FloatComparison.gt" title="scitools.FloatComparison.FloatComparison.gt"><tt class="xref py py-obj docutils literal"><span class="pre">gt</span></tt></a>(a,&nbsp;b[,&nbsp;rtol,&nbsp;atol])</td>
<td>Tests a &gt; b with tolerance.</td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="#scitools.FloatComparison.FloatComparison.le" title="scitools.FloatComparison.FloatComparison.le"><tt class="xref py py-obj docutils literal"><span class="pre">le</span></tt></a>(a,&nbsp;b[,&nbsp;rtol,&nbsp;atol])</td>
<td>Tests a &lt;= b with tolerance.</td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="#scitools.FloatComparison.FloatComparison.lt" title="scitools.FloatComparison.FloatComparison.lt"><tt class="xref py py-obj docutils literal"><span class="pre">lt</span></tt></a>(a,&nbsp;b[,&nbsp;rtol,&nbsp;atol])</td>
<td>Tests a &lt; b with tolerance.</td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="#scitools.FloatComparison.FloatComparison.ne" title="scitools.FloatComparison.FloatComparison.ne"><tt class="xref py py-obj docutils literal"><span class="pre">ne</span></tt></a>(a,&nbsp;b[,&nbsp;rtol,&nbsp;atol])</td>
<td>Tests a != b with tolerance.</td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="#scitools.FloatComparison.FloatComparison.set_absolute_tolerance" title="scitools.FloatComparison.FloatComparison.set_absolute_tolerance"><tt class="xref py py-obj docutils literal"><span class="pre">set_absolute_tolerance</span></tt></a>(atol)</td>
<td></td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="#scitools.FloatComparison.FloatComparison.set_relative_tolerance" title="scitools.FloatComparison.FloatComparison.set_relative_tolerance"><tt class="xref py py-obj docutils literal"><span class="pre">set_relative_tolerance</span></tt></a>(rtol)</td>
<td></td>
</tr>
</tbody>
</table>
<dl class="attribute">
<dt id="scitools.FloatComparison.FloatComparison.atol">
<tt class="descname">atol</tt><em class="property"> = 1e-14</em><a class="headerlink" href="#scitools.FloatComparison.FloatComparison.atol" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="method">
<dt id="scitools.FloatComparison.FloatComparison.eq">
<tt class="descname">eq</tt><big>(</big><em>a</em>, <em>b</em>, <em>rtol=None</em>, <em>atol=None</em><big>)</big><a class="reference internal" href="_modules/scitools/FloatComparison.html#FloatComparison.eq"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#scitools.FloatComparison.FloatComparison.eq" title="Permalink to this definition">¶</a></dt>
<dd><p>Tests a == b with tolerance.</p>
</dd></dl>

<dl class="method">
<dt id="scitools.FloatComparison.FloatComparison.ge">
<tt class="descname">ge</tt><big>(</big><em>a</em>, <em>b</em>, <em>rtol=None</em>, <em>atol=None</em><big>)</big><a class="reference internal" href="_modules/scitools/FloatComparison.html#FloatComparison.ge"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#scitools.FloatComparison.FloatComparison.ge" title="Permalink to this definition">¶</a></dt>
<dd><p>Tests a &gt;= b with tolerance.</p>
</dd></dl>

<dl class="method">
<dt id="scitools.FloatComparison.FloatComparison.get_absolute_tolerance">
<tt class="descname">get_absolute_tolerance</tt><big>(</big><big>)</big><a class="reference internal" href="_modules/scitools/FloatComparison.html#FloatComparison.get_absolute_tolerance"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#scitools.FloatComparison.FloatComparison.get_absolute_tolerance" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="method">
<dt id="scitools.FloatComparison.FloatComparison.get_relative_tolerance">
<tt class="descname">get_relative_tolerance</tt><big>(</big><big>)</big><a class="reference internal" href="_modules/scitools/FloatComparison.html#FloatComparison.get_relative_tolerance"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#scitools.FloatComparison.FloatComparison.get_relative_tolerance" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="method">
<dt id="scitools.FloatComparison.FloatComparison.gt">
<tt class="descname">gt</tt><big>(</big><em>a</em>, <em>b</em>, <em>rtol=None</em>, <em>atol=None</em><big>)</big><a class="reference internal" href="_modules/scitools/FloatComparison.html#FloatComparison.gt"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#scitools.FloatComparison.FloatComparison.gt" title="Permalink to this definition">¶</a></dt>
<dd><p>Tests a &gt; b with tolerance.</p>
</dd></dl>

<dl class="method">
<dt id="scitools.FloatComparison.FloatComparison.le">
<tt class="descname">le</tt><big>(</big><em>a</em>, <em>b</em>, <em>rtol=None</em>, <em>atol=None</em><big>)</big><a class="reference internal" href="_modules/scitools/FloatComparison.html#FloatComparison.le"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#scitools.FloatComparison.FloatComparison.le" title="Permalink to this definition">¶</a></dt>
<dd><p>Tests a &lt;= b with tolerance.</p>
</dd></dl>

<dl class="method">
<dt id="scitools.FloatComparison.FloatComparison.lt">
<tt class="descname">lt</tt><big>(</big><em>a</em>, <em>b</em>, <em>rtol=None</em>, <em>atol=None</em><big>)</big><a class="reference internal" href="_modules/scitools/FloatComparison.html#FloatComparison.lt"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#scitools.FloatComparison.FloatComparison.lt" title="Permalink to this definition">¶</a></dt>
<dd><p>Tests a &lt; b with tolerance.</p>
</dd></dl>

<dl class="method">
<dt id="scitools.FloatComparison.FloatComparison.ne">
<tt class="descname">ne</tt><big>(</big><em>a</em>, <em>b</em>, <em>rtol=None</em>, <em>atol=None</em><big>)</big><a class="reference internal" href="_modules/scitools/FloatComparison.html#FloatComparison.ne"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#scitools.FloatComparison.FloatComparison.ne" title="Permalink to this definition">¶</a></dt>
<dd><p>Tests a != b with tolerance.</p>
</dd></dl>

<dl class="attribute">
<dt id="scitools.FloatComparison.FloatComparison.rtol">
<tt class="descname">rtol</tt><em class="property"> = 1e-14</em><a class="headerlink" href="#scitools.FloatComparison.FloatComparison.rtol" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="method">
<dt id="scitools.FloatComparison.FloatComparison.set_absolute_tolerance">
<tt class="descname">set_absolute_tolerance</tt><big>(</big><em>atol</em><big>)</big><a class="reference internal" href="_modules/scitools/FloatComparison.html#FloatComparison.set_absolute_tolerance"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#scitools.FloatComparison.FloatComparison.set_absolute_tolerance" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="method">
<dt id="scitools.FloatComparison.FloatComparison.set_relative_tolerance">
<tt class="descname">set_relative_tolerance</tt><big>(</big><em>rtol</em><big>)</big><a class="reference internal" href="_modules/scitools/FloatComparison.html#FloatComparison.set_relative_tolerance"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#scitools.FloatComparison.FloatComparison.set_relative_tolerance" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

</dd></dl>

</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="index.html">
              <img class="logo" src="_static/scitools_logo.jpg" alt="Logo"/>
            </a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/FloatComparison.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="np-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="index.html">SciTools 0.8.3 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, H. P. Langtangen, J. Ring, ++.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.2.
    </div>
  </body>
</html>